package com.china08.yxyapi.api.manage.sch.platform.website.image;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSettingSliders;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingSlidersRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class ImageUpdOrderNumController extends YxyApiBaseController {

    @Autowired
    SwsSettingSlidersRepository swsSettingSlidersRepository;

    @ApiOperation(value = "修改排序")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/image/upd/ordernum", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String id = reqModel.id;
        String flag = reqModel.flag;
        String schoolId = getProfile(request, response).getSchoolId();

        SwsSettingSliders swsSettingSliders = swsSettingSlidersRepository.findById(id);
        Assert.isTrue(swsSettingSliders != null, "未获取移动数据");

        Integer orderNum = swsSettingSliders.getOrderNum();
        Integer orderNum2 = null;

        // 交换orderNum对象
        SwsSettingSliders swsSettingSliders2 = new SwsSettingSliders();

        // 上移
        if (flag.equals("up")) {
            // 获取上一条
            List<SwsSettingSliders> list = swsSettingSlidersRepository.findTop1BySchoolIdAndOrderNumLtOrderByOrderNum(schoolId, orderNum);
            Assert.isTrue(list.size() > 0, "不可以移动");
            swsSettingSliders2 = list.get(0);
        }
        // 下移
        else {
            // 获取下一条
            List<SwsSettingSliders> list = swsSettingSlidersRepository.findTop1BySchoolIdAndOrderNumGtOrderByOrderNum(schoolId, orderNum);
            Assert.isTrue(list.size() > 0, "不可以移动");
            swsSettingSliders2 = list.get(0);
        }

        // 交换orderNum
        orderNum2 = swsSettingSliders2.getOrderNum();

        swsSettingSliders.setOrderNum(orderNum2);
        swsSettingSliders2.setOrderNum(orderNum);

        swsSettingSlidersRepository.save(swsSettingSliders);
        swsSettingSlidersRepository.save(swsSettingSliders2);
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 焦点图id
        public String id;

        // 上移下移标记
        public String flag;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(id), "修改对象为空");
            Assert.isTrue(StringUtils.isNotBlank(flag), "状态为空");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
